#!/bin/bash

RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'

COMMIT_MSG_FILE=$(git rev-parse --git-path COMMIT_EDITMSG)
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")

TITLE_LINE=$(echo "$COMMIT_MSG" | head -n1)

REGEX="^(feat|fix|docs|style|refactor|perf|test|build|chore|revert): .+$"

if ! echo "$TITLE_LINE" | grep -qE "$REGEX"; then
    echo -e "${RED}ERROR${NC}: Invalid commit message title format."

    cat <<-EOF

Commit message title must follow the format:

<type>: <description>

Allowed types:
feat:     A new feature
fix:      A bug fix
docs:     Documentation changes
style:    Code style changes
refactor: Code refactoring
perf:     Performance improvements
test:     Test related changes
build:    Build system changes
chore:    Other maintenance changes (including regular backend update)
revert:   Revert a previous commit

Example:
feat: add BM1688 yolov8 postprocess support

Your commit message title:
$TITLE_LINE
EOF
    exit 1
fi

if [[ $(echo "$COMMIT_MSG" | wc -l) -gt 1 ]]; then
    SECOND_LINE=$(echo "$COMMIT_MSG" | sed -n '2p')
    if [[ -n "$SECOND_LINE" ]]; then
        echo -e "${RED}ERROR${NC}: There must be a blank line between title and body"
        exit 1
    fi
fi

if [[ $(echo "$COMMIT_MSG" | wc -l) -gt 2 ]]; then
    BODY_START=3
    BODY=$(echo "$COMMIT_MSG" | tail -n +$BODY_START)

    FIRST_BODY_LINE=$(echo "$BODY" | head -n1)
    if [[ ! $FIRST_BODY_LINE =~ ^- ]]; then
        echo -e "${RED}ERROR${NC}: Body must start with bullet points (using '-')"
        exit 1
    fi

    if [[ $COMMIT_MSG == *"BREAKING CHANGE"* ]]; then
        if [[ $COMMIT_MSG != *"BREAKING CHANGE:"* ]]; then
            echo "${RED}ERROR${NC}: Breaking changes must include 'BREAKING CHANGE:' prefix"
            exit 1
        fi
    fi
fi
echo -e "${GREEN}Success: commit message format check ok${NC}"

exit 0
